#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<b;++i)
#define repp(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
#define pper(i,a,b) for(int i=a;i>=b;--i)
#define pii pair<int,int>
#define m_p make_pair
#define fi first
#define se second
#define pb push_back
#define inf 0x7fffffff
#define inff 0x7fffffffffffffff
#define all(x) x.begin(),x.end()
#define int ll
const int mod = 998244353;
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')f=-f;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=x*10+(ch^48);
		ch=getchar();
	}
	return x*f;
}
ll pw(ll x,int d){
	ll t=1;
	for(;d;d>>=1,x=x*x%mod)if(d&1)t=t*x%mod;
	return t;
}
#define MAX 1000005
int a[MAX];
int tong[MAX];
int dp[MAX];
int pre[MAX];
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int T = rd();
	while(T--){
		int n = rd();
		repp(i,1,n)a[i] = rd();
		a[n+1] = MAX-2;
		memset(dp,~(0x3f),sizeof(dp));
		memset(tong,~(0x3f),sizeof(tong));
		memset(pre,0,sizeof(pre));
		repp(i,1,n+1)pre[i] = pre[i-1]+(a[i]==a[i-1])*a[i];
		dp[1] = 0;
		int lst = 0-pre[1];
		repp(i,2,n+1){
			// dp[i] = dp[i-1]+a[i]*(a[i]==a[i-2]);
			// dp[i] = max(dp[i],tong[a[i]]+a[i]+(a[i]==a[i-1])*a[i-1]);
			// dp[i] = max(dp[i],lst+(a[i]==a[i-1])*a[i-1]);
			// tong[a[i-2]] = max(tong[a[i-2]],dp[i-1]);
			// lst = max(lst,dp[i-1]);
			dp[i] = max(lst,tong[a[i]]+a[i])+pre[i-1];
			tong[a[i-1]] = max(tong[a[i-1]],dp[i]-pre[i]);
			lst = max(lst,dp[i]-pre[i]);
		}
		// repp(i,1,n)cout<<dp[i]<<' ';cout<<endl;
		cout<<dp[n+1]<<endl;
	}
	return 0;
}
/*
g++ color.cpp -o color -g -std=c++14 -Wall -Wshadow -fsanitize=address,undefined && ./color < color1.in 
*/